package mlp;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;

import function.SigmoidalFunction;


public class Training {


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] layersSizes = {7, 6, 1};

		Data data = new Data(".\\seedTreino.txt");
		ArrayList<Element> elementos = data.elementos;
		int contador = 0;
		double[][] inputs = new double[elementos.size()][4];
		double[][] outputs = new double[elementos.size()][1];
		double[] classeSaida = new double[1];
		for (Element a : elementos){
			inputs[contador] = a.getInput();
			classeSaida[0] = a.getClasse();
			outputs[contador] = classeSaida.clone();
			contador++;
		}
		data = new Data(".\\seedValidacao.txt");
		ArrayList<Element> elementosValidacao = data.elementos;
		contador = 0;
		double[][] inputsVal = new double[elementosValidacao.size()][4];
		double[][] outputsVal = new double[elementosValidacao.size()][1];
		classeSaida = new double[1];
		for (Element a : elementosValidacao){
			inputsVal[contador] = a.getInput();
			classeSaida[0] = a.getClasse();
			outputsVal[contador] = classeSaida.clone();
			contador++;
		}

		MLP nn = null;
		double[] saida;
		double erro;
		for (int i = 0; i < 10; i++) {
			
			erro = 0;
			nn = new MLP(layersSizes, new SigmoidalFunction(), 1 );
			nn.onlineTraining(inputs, outputs, true, 0.35, 0, 5000, 0.01, 0, 1);	
			System.out.println("EMQ treino = " + nn.getErrorData()[0] );
			data = new Data(".\\seedValidacao.txt");
			for (Element a : data.elementos) {
				saida = nn.calculateOutput(a.getInput(), true);
				double[] classe = {a.getClasse()};
				erro += nn.calcularErroPercentual(saida, classe);
			}	
			System.out.println("Classificao Validacao = " + erro/(double)data.elementos.size());
			
		}

	}

	private static double[][] readInput(String path){
		double[][] retorno = null;
		try {
			ArrayList<double[]> listaTemp = new ArrayList();
			File file = new File(path);
			Scanner reader = new Scanner(file);

			while(reader.hasNextLine()){
				String[] line = reader.nextLine().split("\t");
				double[] values = new double[2]; //Pin, Pout, Nf, Rp 

				values[0] = Double.parseDouble(line[0]);
				values[1] = Double.parseDouble(line[1]);

				listaTemp.add(values);
			}

			retorno = new double[listaTemp.size()][2];

			for(int i=0; i<retorno.length; i++){
				retorno[i] = listaTemp.get(i);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return retorno;
	}

	private static double[][] readOutput(String path){
		double[][] retorno = null;
		try {
			ArrayList<double[]> listaTemp = new ArrayList();
			File file = new File(path);
			Scanner reader = new Scanner(file);

			while(reader.hasNextLine()){
				String[] line = reader.nextLine().split("\t");
				double[] values = new double[2]; //Pin, Pout, Nf, Rp 

				values[0] = Double.parseDouble(line[2]);
				values[1] = Double.parseDouble(line[3]);

				listaTemp.add(values);
			}

			retorno = new double[listaTemp.size()][2];

			for(int i=0; i<retorno.length; i++){
				retorno[i] = listaTemp.get(i);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return retorno;
	}

}
